﻿using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Reborn.Net.Http
{
    public class Response 
    {


        /// <summary>
        /// 内容类型
        /// </summary>

        public string? ContentType { get; set; }

        /// <summary>
        /// 内容长度
        /// </summary>
        public long? ContentLength { get; set; }


        /// <summary>
        /// 内容编码
        /// </summary>
        public ICollection<string> ContentEncoding { get; set; } = new List<string>();


        /// <summary>
        /// 返回内容
        /// </summary>
        public string? Content { get; set; }

        /// <summary>
        /// 内容转换成JSON
        /// </summary>
        public JToken ToJson
        {
            get
            {
                try
                {
                    return JToken.Parse(Content);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// HTTP状态码
        /// </summary>
        public HttpStatusCode StatusCode { get; set; }

        /// <summary>
        /// 是否成功的代码
        /// </summary>
        public bool IsSuccessStatusCode { get; set; }


        /// <summary>
        /// Whether or not the HTTP response status code indicates success and no other error   occurred (deserialization, timeout, ...)
        /// </summary>
        public bool IsSuccessful
        {
            get
            {
                if (IsSuccessStatusCode)
                {
                    return StatusCode == HttpStatusCode.OK;
                }

                return false;
            }
        }


        /// <summary>
        /// 状态码描述
        /// </summary>
        public string? StatusDescription { get; set; }


        /// <summary>
        /// 返回原始数据
        /// </summary>
        public byte[]? RawBytes { get; set; }


        /// <summary>
        /// 返回URI
        /// </summary>
        public Uri? ResponseUri { get; set; }


        /// <summary>
        /// HttpWebResponse.Server
        /// </summary>
        public string? Server { get; set; }

        /// <summary>
        /// Cookies returned by server with the response
        /// </summary>
        public CookieCollection? Cookies { get; set; }


        /// <summary>
        ///  Response headers returned by server with the response
        /// </summary>
        public IReadOnlyCollection<HttpHeaders>? Headers { get; set; }


        /// <summary>
        /// Content headers returned by server with the response
        /// </summary>
        public IReadOnlyCollection<HttpHeaders>? ContentHeaders { get; set; }



        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 错误
        /// </summary>
        public Exception? ErrorException { get; set; }


        /// <summary>
        /// HTTP protocol version of the request
        /// </summary>
        public Version? Version { get; set; }

        //
        // 摘要:
        //     Root element of the serialized response content, only works if deserializer supports
        //     it
        public string? RootElement { get; set; }

        //
        // 摘要:
        //     Default constructor
        public Response()
        {
            StatusCode = HttpStatusCode.Created;
        }

        //
        // 摘要:
        //     Assists with debugging responses by displaying in the debugger output
        protected string DebuggerDisplay()
        {
            //DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(47, 3);
            //defaultInterpolatedStringHandler.AppendLiteral("StatusCode: ");
            //defaultInterpolatedStringHandler.AppendFormatted(StatusCode);
            //defaultInterpolatedStringHandler.AppendLiteral(", Content-Type: ");
            //defaultInterpolatedStringHandler.AppendFormatted(ContentType);
            //defaultInterpolatedStringHandler.AppendLiteral(", Content-Length: ");
            //defaultInterpolatedStringHandler.AppendFormatted(ContentLength);
            //defaultInterpolatedStringHandler.AppendLiteral(")");
            //return defaultInterpolatedStringHandler.ToStringAndClear();

            return string.Empty;
        }

        public Exception? GetException()
        {
            return StatusCode switch
            {
                HttpStatusCode.InternalServerError => ErrorException,
                HttpStatusCode.RequestTimeout => new TimeoutException("Request timed out", ErrorException),
                HttpStatusCode.Created => null,
                HttpStatusCode.OK => null,
                _ => throw ErrorException ?? new ArgumentOutOfRangeException("ResponseStatus"),
            };
        }
    }
}
